import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class ResUsersOdooApi{

  static Future<ResponseBodyApi> login(data) async {
    ResponseBodyApi responseBodyApi;
    try{
      var res = await OdooHttpUtil.createInstance(data);
      responseBodyApi = ResponseBodyApi(success: true,data: res);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try{
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(domain: domainList,limit: (pageModel.current-1)*pageModel.size,offset: pageModel.current-1,order: pageModel.orders[0].column);
      var records = await OdooHttpUtil.callKw(model:'res.user',method:'search_read',kwargs:recordsKwargs.toMap());
      var total = await OdooHttpUtil.callKw(model:'res.user',method:'search_count',args: [domainList]);
      pageModel.records = records;
      pageModel.total = total;
      pageModel.pages = total/pageModel.size;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try{
      await OdooHttpUtil.callKw(model: 'res.user',method: "unlink", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try{
      if(data["id"]==null){
        await OdooHttpUtil.callKw(model: 'res.user',method: 'create', args: [data]);
      }else{
        await OdooHttpUtil.callKw(model: 'res.user',method: 'write', args: [[data["id"]],data]);
      }
      responseBodyApi = ResponseBodyApi(success: true);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static getCurrentUserInfo(userId) async{
    ResponseBodyApi responseBodyApi;
    try{
      Kwargs kwargs = new Kwargs(domain: [['user_id', '=', userId],],fields: [
          'id',
          'name',
          'user_id',
          'company_id',
          'department_id',
          'teams_info',
          'employee_type'
          ]);
      var res = await OdooHttpUtil.callKw(model: "hr.employee", method: "search_read",kwargs: kwargs.toMap());
      responseBodyApi = ResponseBodyApi(success: true,data: res);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

}